﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Experiment.DAL;
using System.IO;
using System.Reflection;

namespace Experiment.BLL
{
    public static class Controller
    {


        public static List<User> CreateUsers()
        {
            string name;
            List<int> docs = new List<int>();
            List<User> allUsers = new List<User>();

            string[] csvText = Properties.Resources.experiment_user_emails_sequence.Split('\n');

            foreach (string line in csvText)
            {
                string[] csvRow = line.Split(',');
                name = csvRow[0];

                for (int i = 1; i < csvRow.Length; i++)
                {
                    docs.Add(int.Parse(csvRow[i]));
                }


                User newUser = new User(name, docs);
                allUsers.Add(newUser);
            }

            return allUsers;

            //using (CsvFileReader reader = new CsvFileReader(@"..\..\..\Experiment\BLL\experiment_user_emails_sequence.csv"))
            //{
            //    CsvRow row = new CsvRow();
            //    while (reader.ReadRow(row))
            //    {
            //        name = row[0];
            //        for (int i = 1; i <= row.Count; i++)
            //        {
            //            docs.Add(int.Parse(row[i]));
            //        }


            //        User newUser = new User(name, docs);


            //    }
            //}
            //return allUsers;


        }





        public static List<Document> displayDocs(User user)
        {

            List<Document> allDoc = new List<Document>();
            foreach (int i in user.UserList)
            {
                string subject = DataQueries.GetMailSubject(i.ToString());
                string content = DataQueries.GetMailContent(i.ToString());
                string fixedContent = content.Replace("\n", "<br />");
                Document d1 = new Document(subject, fixedContent);
                allDoc.Add(d1);

            }
            return allDoc;

        }


    }


    public class CsvRow : List<string>
    {
        public string LineText { get; set; }
    }


    public class CsvFileReader : StreamReader
    {
        public CsvFileReader(Stream stream)
            : base(stream)
        {
        }

        public CsvFileReader(string filename)
            : base(filename)
        {
        }

        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
                return false;

            int pos = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                        pos++;
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                    row[rows] = value;
                else
                    row.Add(value);
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    pos++;
                if (pos < row.LineText.Length)
                    pos++;
            }
            // Delete any unused items
            while (row.Count > rows)
                row.RemoveAt(rows);

            // Return true if any columns read
            return (row.Count > 0);
        }
    }
}